package com.waves.task.strategy.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.waves.core.constant.DigitConstant;
import com.waves.core.constant.redis.UserRedisKey;
import com.waves.core.utils.LoginUtils;
import com.waves.core.utils.RedisUtils;
import com.waves.task.domain.dto.TaskPageDTO;
import com.waves.task.domain.enums.TaskTypeEnum;
import com.waves.task.domain.vo.TaskVO;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 关注贴策略类
 */
@Component
public class TaskFollowStrategy extends AbstractTaskStrategy{
    @Override
    TaskTypeEnum getTaskTypeEnum() {
        return TaskTypeEnum.TASK_TYPE_FOLLOW;
    }
    @Override
    public TaskPageDTO dealTaskPageDTO(TaskPageDTO dto) {
        // 查询当前用户关注的人的帖子
        Long userId = LoginUtils.getUserId();
        String followKey = UserRedisKey.getKey(UserRedisKey.USER_FOLLOW_SET, userId);
        Set<String> strings = RedisUtils.sGet(followKey);
        assert strings != null;
        if (CollUtil.isNotEmpty(strings)) {
            List<Long> longList = strings.stream()
                    .map(Long::parseLong)
                    .collect(Collectors.toList());
            dto.setUserIds(longList);
        }else {
            dto.setUserIds(Collections.singletonList(DigitConstant.DIGIT_UID));
        }
        return dto;
    }

    @Override
    public IPage<TaskVO> getTaskVOPage(TaskPageDTO dto) {
        return super.searchAndDealPage(dealTaskPageDTO(dto));
    }


}
